<?php
session_start();
if (isset ($_GET['message'])){
echo "SNUM:" . rand(1,25);
}
if (isset ($_GET['CMD'])){
    $cltcmd= $_GET['CMD'];
	if (isset($_GET['value'])) // user set matrix
	{
		//echo "SDATA:".$_GET['value'];
        //Create server array of numbers.
        $smat= createservermat();
        //print_r($smat);
		//echo 'SDATA:';
		endgame();
		$clientmat = split(',',$_GET['value']);
		unset($clientmat[25]);//clear last one ,
    	//print_r($clientmat);
    	$gamearr=array(); // new array.

		$_SESSION['GMAT'] =  $gamearr;
        if(!isset($_SESSION['SMAT']))
        {
               $_SESSION['SMAT'] = $smat;
               //echo (':value set');
        }
		else
		{
				$smat = $_SESSION['SMAT'];

		}
		$cval=getnew($smat,1);
		$sval=pollbutton($cval);
		echo 'CMD:GAMESTART:'.$cval;

	}
	else if ( $cltcmd =='token') // server have the token.
	{
		$cval =$_GET['cval'];
		$sval=pollbutton($cval);
		if($sval){ echo 'CMD:WIN:'.print_r($_SESSION['SMAT']); return;}
		$gamearr= $_SESSION['GMAT'] ;
		$gamearr[]=$cval; //push data
		$_SESSION['GMAT'] =  $gamearr;
		//print getnew($_SESSION['GMAT']);
		$newnum=getnew($_SESSION['SMAT'],1);
		$sval=pollbutton($newnum);


		if($sval){ echo 'CMD:WIN:token:'.$newnum.':'.print_r($_SESSION['SMAT']); return;}
		echo 'CMD:token:'.$newnum;
		print_r($_SESSION['GMAT']);
	}
	else if ($cltcmd='win')
	{
		$smat=$_SESSION['SMAT'];
		 $txt="TT,";
		echo 'CMD:info:You win the game!:sdat:';
		foreach($smat as $sval)
		{echo ($sval."," );}
		endgame();
	}

}
else
{
	print_r("Testing...");
       $rowhash=array();$colhash=array();
	   $smat= createservermat();
	   //pollbutton(10);

	         if(!isset($_SESSION['SMAT']))
	         {
	                $_SESSION['SMAT'] = $smat;
	                echo ('value set');
        }
	//print_r($arr );
	 endgame();

}

// Set the number and check for win.
function pollbutton($num)
{
		$srow =$_SESSION['SROW'] ; $scol=$_SESSION['SCOL'];
		$sdig = $_SESSION['SDIG'] ;
		$srowhash = $_SESSION['SROWHASH'] ; $scolhash = $_SESSION['SCOLHASH'] ;
		$row = $srowhash[print_r($num,true)];$col = $scolhash[print_r($num,true)];
		$srow[($row-1)]++; $scol[($col-1)]++;

		if($row==$col)$sdig[0]++;
		if($row==6-$col)$sdig[1]++;
		$_SESSION['SDIG'] =$sdig;
		$_SESSION['SROW'] =$srow; $_SESSION['SCOL']=$scol;
		return checkwin(array($srow,$scol,$sdig));
}
function checkwin($matdat)
{
$tot=0;
for($i=0;$i<2;$i++)
{
   $arr=$matdat[$i];
   for($j=0;$j<5;$j++)
   {
   if($arr[$j]>=5)$tot++;
   }
}
$arr=$matdat[3];
if($arr[0]>=5)$tot++;
if($arr[1]>=5)$tot++;
if($tot>=5)return true;
return false;
}

function endgame()
{
unset($_SESSION['SMAT']);
unset($_SESSION['GMAT']);
}

// Hold an array for clicked.
// Hold an array for actual.
// Getnew returns a new number not clicked.

function getnew($source ,$level )
{

 $gamearr= $_SESSION['GMAT'];
 $num=0;
 if(isset( $_SESSION['GMAT'])) {
 				if(count( $gamearr)>=25){
 					echo 'array:';
 					print_r($gamearr);return -1;
 				}
		 // get a    new random number which is not in array.
		$f==0;
		if($level==1)//level 1
		{
		while($f==0) {$num = rand(1,25);
        	if (!in_array($num,$gamearr)) //not in array add it
        		{
			 	$f=1;
			 	$gamearr[]=$num;
			 	 $_SESSION['GMAT'] =  $gamearr;

			}
		 }
		}
   }
		return $num . ':'.count($gamearr) ;

}



function createservermat()
{
	$srow=array(0,0,0,0,0);$scol=array(0,0,0,0,0);
	$sdig=array(0,0);

	$arr=array();
    $row=1;	$col=1;
	for($i=1;$i<=25;$i++)
	{
   	$arr=returnnewrand($arr);
	$rowhash[print_r( $arr[ count($arr)-1],true) ] =$row;
	$colhash[print_r( $arr[ count($arr)-1],true) ] =$col;
	//echo '<br>'.$row.",".$col.'</br>';

	if($col%5==0){$col=1; $row++;}
    else $col++;
	//$row++;
	}
	//for(

	/*for($i = 1 ; $i<=25; $i++)
	{
	$val = array_search($i,$arr);
	print "Found $i at $val </br>";
	}*/
	$_SESSION['SROWHASH']=$rowhash;
	$_SESSION['SCOLHASH']=$colhash;

		$_SESSION['SROW']=$srow;		$_SESSION['SCOL']=$scol;
		$_SESSION['SDIG']=$sdig;
	return $arr;
}

function returnnewrand($arr)
{
	$f=0;
    while($f==0)
    {
		$num = rand(1,25);
        if (!in_array($num,$arr)) //not in array add it
        {
          $arr[]=$num;
          $f=1;

        }


	}
	return $arr;
}
?>